package com.cilico.single.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings;

import com.cilico.single.ConfigManager;
import com.cilico.single.R;
/**
 * 系统设置界面
 * @author lvke
 * Email: lvke@live.cn
 * 2014-1-16下午4:23:12
 */
public class SysSeting extends PreferenceActivity{

	
	PreferenceScreen screen_;
	/**设置网络域*/
	EditTextPreference etp_;
	/**外网*/
	CheckBoxPreference cbx_net_;
	/**倒数说明域*/
	Preference pre_;
	ChangListen listenChang_;
	ClickListen listenClick_;
	/**是不是程序启动的时候进来的*/
	boolean isStart_ = false;
	MyCount myCount_;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		screen_= getPreferenceScreen();
		listenChang_ = new ChangListen();
		listenClick_ = new ClickListen();
		etp_ = (EditTextPreference) this.findPreference("set_host");
		etp_.setOnPreferenceChangeListener(listenChang_);
		etp_.setOnPreferenceClickListener(listenClick_);
		
		cbx_net_ = (CheckBoxPreference) this.findPreference("net");
		cbx_net_.setChecked(true);
		cbx_net_.setOnPreferenceChangeListener(listenChang_);
		cbx_net_.setOnPreferenceClickListener(listenClick_);
		
		pre_ = this.findPreference("text");
		
		etp_.setEnabled(!cbx_net_.isChecked());
		init();
	}
	
	/**
	 * 初始化。主要过滤程序启动的时候
	 * @author lvke
	 * Email: lvke@live.cn
	 * 2014-1-15下午4:19:26
	 */
	private void init(){
		Intent intent = getIntent();
		if (intent != null) {
			Bundle requestdata = intent.getExtras();
			if (requestdata != null) {
				isStart_ = requestdata.getBoolean("start");
			}
		}
		if (isStart_) {
			SplashScreen.gotoStart_ = true;
			SplashScreen.haveSetNet_ = true;
			myCount_ = new MyCount(4000, 1000);
			myCount_.start();
		}else {
			screen_.removePreference(pre_);
		}
	}
	
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		if (myCount_ != null) {
			myCount_.close();
		}
		super.finish();
	}
	
	class ChangListen implements OnPreferenceChangeListener{

		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			if ( preference.getKey().contains("set_host"))
			{
				String value = (String) newValue;
				if (value != null && !value.trim().equals("")) {
					if (!value.startsWith("http://")) {
						value = value + "http://";
					}
					ConfigManager.SET_HOST_URL = value;
				}
			}else if (preference.getKey().contains("net")) {
				etp_.setEnabled(cbx_net_.isChecked());
			}
			return true;
		}
		
	}
	
	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {
		// TODO Auto-generated method stub
		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}
	
	class ClickListen implements OnPreferenceClickListener{

		@Override
		public boolean onPreferenceClick(Preference preference) {
			if (preference.getKey().contains("set_apn")) {
				Intent setInt = new Intent(Settings.ACTION_APN_SETTINGS);
				SysSeting.this.startActivity(setInt);
				finish();
			}
			if (myCount_ != null) {
				myCount_.close();
			}
			return true;
		}
		
	}
	/**
	 * 倒数计时
	 * @author lvke
	 * Email: lvke@live.cn
	 * 2014-1-16上午11:13:46
	 */
	class MyCount extends CountDownTimer{

		public MyCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			finish();
		}

		@Override
		public void onTick(long millisUntilFinished) {
			// TODO Auto-generated method stub
			pre_.setTitle("触摸屏幕可以设置网络    " + millisUntilFinished/1000 +"秒");
		}
		
		public void close(){
			this.cancel();
			pre_.setTitle("设置完成后,按返回键让程序继续");
			if (myCount_ != null) {
				myCount_.cancel();
				myCount_ = null;
			}
		}
		
	}
}